home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / lang / mc302 / cutil / shell.c < prev   
C/C++ Source or Header  |  1994-03-18  |  3KB  |  125 lines

  1. /*
  2.  * General purpose user interface shell which supports both
  3.  * command line and interactive execution modes.
  4.  *
  5.  * Copyright 1991-1994 Dave Dunfield
  6.  * All rights reserved.
  7.  *
  8.  * Permission granted for personal (non-commercial) use only.
  9.  *
  10.  * Compile command: cc shell -fop
  11.  */
  12. #include <stdio.h>
  13.  
  14. #define    ARG_NUM        50        /* Maximum number of command arguments */
  15. #define    ARG_POOL    500        /* Memory reserved for prompt strings */
  16.  
  17. /* Table of command names */
  18. char *commands[] = {
  19.     "quit", "help"
  20.     /* Add additional command names here */
  21.     };
  22.  
  23. /* Help display text */
  24. char *help[] = {
  25.     "SHELL commands:\n"
  26.     /* Add HELP text here */
  27.     };
  28.  
  29. /* Command shell internal variables */
  30.     int argc, argt;
  31.     char *argv[ARG_NUM], argp[ARG_POOL], *optr;
  32.     
  33. /*
  34.  * Process command from operand or command line
  35.  */
  36. main(ac, av)
  37.     int ac;
  38.     int *av[];
  39. {
  40.     int i;
  41.     char cmdline[100];
  42.  
  43.     if(!ac)
  44.         abort("Use: shell <options>\n");
  45.  
  46.     argc = 0;
  47.     for(i=1; i < ac; ++i) switch(*av[i]) {
  48. /*** Parse main program command line options here ***/
  49.         default: argv[argc++] = av[i]; }
  50.  
  51.     if(argc)                /* Command line mode */
  52.         command();
  53.     else for(;;) {            /* Interactive mode */
  54.         fputs("SHELL> ", stdout);
  55.         fflush(stdout);
  56.         if(!fgets(optr = cmdline, sizeof(cmdline)-1, stdin))
  57.             exit(0);
  58.         argc = 0;
  59.         while(skip_blank()) {
  60.             argv[argc++] = optr;
  61.             while(*optr && !isspace(*optr))
  62.                 ++optr;
  63.             if(*optr)
  64.                 *optr++ = 0; }
  65.         if(!argc)
  66.             continue;
  67.         command(); }
  68. }
  69.  
  70. /* Skip to next non-blank */
  71. skip_blank()
  72. { while(isspace(*optr)) ++optr; return *optr; }
  73.  
  74. /* Match partial string */
  75. match(s1, s2) char *s1, *s2;
  76. { do if(!*s2) return -1; while(*s1++ == *s2++); return 0; }
  77.  
  78. /* Get argument 'n'. If argument does not exist, issue prompt */
  79. /* Return with 0 if no entry. */
  80. getarg(n, prompt)
  81.     int n;
  82.     char *prompt;
  83. {
  84.     char buffer[50];
  85.  
  86.     if(!argv[n]) {
  87.         argv[n] = &argp[argt];
  88.         fprintf(stdout,"%s? ", prompt);
  89.         fflush(stdout);
  90.         if(!fgets(optr = buffer, sizeof(buffer)-1, stdin))
  91.             exit(0);
  92.         if(!skip_blank())
  93.             return -1;
  94.         do
  95.             argp[argt++] = *optr;
  96.         while(*optr++); }
  97.     return 0;
  98. }
  99.  
  100. /*
  101.  * User program command processor
  102.  */
  103. command()
  104. {
  105.     int i, j;
  106.  
  107.     j = -1;
  108.     for(i=argc; i < ARG_NUM; ++i)            /* Zero remaining arguments */
  109.         argv[i] = 0;
  110.     for(argt=i=0; i < sizeof(commands)/2; ++i)    /* Lookup command in table */
  111.         if(match(commands[i], *argv)) {
  112.             if(j != -1) {
  113.                 fputs("SHELL: Ambiguous command.\n", stderr);
  114.                 return; }
  115.             j = i; }
  116.     switch(j) {                                /* Execute command */
  117.         case 0 : exit(0);                    /* Exit */
  118.         case 1 :                            /* Help */
  119.             for(i=0; i < sizeof(help)/2; ++i)
  120.                 fprintf(stdout,"%s\n", help[i]);
  121.             break;
  122. /** Insert additional command handlers here ***/
  123.         default: fputs("SHELL: Unknown command.\n", stderr); }
  124. }
  125.